package com.example.capsule_chat0322.request;

import android.os.Handler;

import com.example.capsule_chat0322.bean.FunctionBean;
import com.example.capsule_chat0322.bean.UserInfoBean;
import com.example.capsule_chat0322.configuration.HttpConstant;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @Author zhuzhiming
 * @Date 2022/5/22 17:17
 */
public class FriendRequest {

    public static void doGetUserFriend(Handler handler, String userId) {

        if (userId == null) {
            return;
        }

        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .url(HttpConstant.HTTP_PREFIX + "/getFriend/" + userId)
                .build();

        Call call = httpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if (response.code() != 200) return;

                String user = response.body().string();
                List<UserInfoBean> userInfoBeanList = new Gson().fromJson(user, new TypeToken<List<UserInfoBean>>() {
                }.getType());

                if (userInfoBeanList == null) return;

                handler.sendMessage(handler.obtainMessage(1, userInfoBeanList));

            }
        });
    }


    public static void getFriendFunctionList(Handler handler, String userId) {


        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .url(HttpConstant.HTTP_PREFIX + "/getFriendFunctionList/" + userId)
                .build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if(response.code() != 200){
                    return;
                }

                String json = response.body().string();
                List<FunctionBean> functionBeans = new Gson().fromJson(json, new TypeToken<List<FunctionBean>>() {
                }.getType());

                if (functionBeans == null || functionBeans.size() == 0) {
                    return;
                }

                handler.sendMessage(handler.obtainMessage(1, functionBeans));
            }

        });

    }

}
